package com.godofgamblers.hotelmanagementsystem.mapper;

import com.godofgamblers.hotelmanagementsystem.pojo.Reservation;
import com.godofgamblers.hotelmanagementsystem.pojo.Reservation_orderItem;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @author Haonan
 * @date 2019/10/12 - 17:39
 */
@Repository
public interface ReservationMapper {
    // 用客户的名字来查询订单
    @Select("SELECT order_id,order_num,`order`.user_id,`user`.user_name,`user`.user_phone,pay_way,order_state,leave_message_context,leave_message_state,`order`.flag FROM `order` LEFT JOIN `user` ON `order`.user_id = `user`.user_id  WHERE `order`.user_id = any(SELECT user.user_id from user WHERE user_account LIKE CONCAT('%',#{username},'%')) AND `order`.flag = 1")
    public List<Reservation> findCurrentReservationByUsername(String username);

    // 用客户的手机号来查询订单
    @Select("SELECT order_id,order_num,`order`.user_id,`user`.user_name,`user`.user_phone,pay_way,order_state,leave_message_context,leave_message_state,`order`.flag FROM `order` LEFT JOIN `user` ON `order`.user_id = `user`.user_id  WHERE `order`.user_id = any(SELECT user.user_id from user WHERE user_phone LIKE CONCAT('%',#{phone},'%')) AND `order`.flag = 1")
    public List<Reservation> findCurrentReservationByPhone(String phone);

    // 用订单id号来查询订单
    @Select("SELECT order_id,order_num,`order`.user_id,`user`.user_name,`user`.user_phone,pay_way,order_state,leave_message_context,leave_message_state,`order`.flag FROM `order` LEFT JOIN `user` ON `order`.user_id = `user`.user_id WHERE `order`.order_id = #{order_id} AND `order`.flag = 1")
    public Reservation findCurrentReservationByOrderId(int order_id);

    // 用订单编号找到订单下的订单项
    @Select("SELECT order_item_id,order_id, order_item.room_type_id,room_type.room_type_name, plan_stay_time, plan_leave_time, order_item_state, order_item.flag, order_item_state FROM order_item LEFT JOIN room_type ON order_item.room_type_id = room_type.room_type_id WHERE order_id = #{orderID} AND order_item.flag = 1")
    public List<Reservation_orderItem> findCurrentReservationItemByOrderId(int orderID);

    // 用订单项编号找到订单项
    @Select("SELECT order_item_id,order_id, order_item.room_type_id,room_type.room_type_name, plan_stay_time, plan_leave_time, order_item_state, order_item.flag, order_item_state FROM order_item LEFT JOIN room_type ON order_item.room_type_id = room_type.room_type_id WHERE order_item_id = #{order_item_id} AND order_item.flag = 1")
    public Reservation_orderItem findReservationItemByOrderItemId(int order_item_id);

    // 软删除后端选中的客户预定
    @Update("UPDATE order_item SET flag = 0 WHERE order_item_id = #{order_item_id}")
    public boolean updateCurrentReservationItemFlagByOrderId(String order_item_id);

    // 软删除订单项
    @Update("UPDATE `order` SET flag = 0 WHERE order_id = #{order_id}")
    public boolean updateCurrentReservationFlagByOrderId(int order_id);

    // 找出所有存在未读留言的订单
    @Select("SELECT `order`.*,`user`.user_name,`user`.user_phone FROM `order` LEFT JOIN `user` on `order`.user_id = `user`.user_id  WHERE leave_message_state = 0")
    public List<Reservation> findUnreadComment();

    // 更新新预定信息，让消息变成已读
    @Update("UPDATE `order` SET leave_message_state = 1 WHERE order_id = #{order_id}")
    public boolean updateOrderMessageState(int order_id);

    // 更新新预定信息，让所有消息变成已读
    @Update("UPDATE `order` SET leave_message_state = 1")
    public boolean updateOrderAllMessageState();
}
